<?php
/**
 * site 企业站站点设置
 * @author	 kenny
 * @copyright Copyright (c) 2008-3-31
 */

require_once 'mod_base'.SYS_SCRIPT_EXT;
/**
 * @see dp_dp
 */

class mod_site extends mod_base
{
	protected $db;		//

    const TB_SITE       = 'u_content_site ';
	const TB_SITE_TPL   = 'u_content_template';
	const TB_ARTICLE    = 'u_content_article';
    const TB_SITE_INFO  = 'u_content_site_info';
	const TB_SORT       = 'u_content_sort';
	const TB_GSORT      = 'u_base_game';
	const TB_ARTICLE_FRONTPAGE = 'u_content_frontpage';
	/**
	 * 构造
	 * @access public
	 * @return void
	 */
	public function __construct()
	{       
		parent::__construct();	
	}

	/**
	 * 析构
	 * @access public
	 * @return void
	 */
	public function __destruct()
	{
		parent::__destruct();
 	}

	/**
	 * 过滤器
	 * @access public
	 * @param $string stirng
	 * @return stirng
	 */	
    public function filtration($string='')
	{ 
		if ($string) {
			//echo $string;
			$string = trim($string);
			$string = addslashes($string);
			$string = htmlspecialchars($string);
	    }
        return $string;
	}

	/**
	 * 根据域名得到站点信息
	 * @access public
	 * @return array
	 */	
	 public function getSiteInfo($domain)
	 {  
 
		$sql="select a.*, b.*,c.apply_id from ".self::TB_SITE." a 
			left join ".self::TB_SITE_INFO." b on a.site_id=b.site_id 
			LEFT JOIN u_apply c ON (a.user_account=c.user_account)
			where a.site_domain='$domain'";
		//echo $sql;die;
		$result = $this->db->getone($sql);
		return $result;
	 }

	/**
	 * 获得模板
	 * @access public
	 * @param $sortId int
	 * @param $tplId  int
	 * @return array
	 */	
	public function getTpl($tplId=1)
	{    
		 if ($tplId=='') $tplId=1;
		 $sql = 'select * from '. self::TB_SITE_TPL .' where tempid='.$tplId;
         $result = $this->db->getone($sql); 
         return $result;
	}

	/**
	 * 
	 * @access public
	 * @param $tplId  int
	 * @return array
	 */	
	public function getnewslist($sortid, $gsortid='', $limit=5, $start=0)
	{    
		$where="";
		if (intval($sortid)!=5 && intval($sortid)!='') {
			$where .=" and arti_sortid=$sortid";
		}
		if (intval($gsortid)) {
			$where .=" and arti_gsortid=$gsortid";
		}
		if ($start) {
			$limit = "$start,$limit";
		}
		$sql = sprintf("select article_id, title, time, pic_path from ".self::TB_ARTICLE." where 1=1".$where." order by time desc limit $limit");
		//echo $sql;die;
        $row = $this->db->query( $sql);
		$result = $row->fetchAll( PDO::FETCH_ASSOC);
		return $result;		
	} 

	public function getactivlist($gsortid='', $limit=5)
	{    
		$where="";
		if (intval($gsortid)) {
			$where .=" and arti_gsortid=$gsortid";
		}
		$where .=" and flash_pic=0";

		$sql = sprintf("SELECT url_path, title, pic_path FROM ".self::TB_ARTICLE_FRONTPAGE." where 1".$where." order by time desc limit $limit");
		//echo $sql;die;
		//echo $sql;
        $row = $this->db->query( $sql);
		$result = $row->fetchAll( PDO::FETCH_ASSOC);
		return $result;		
	} 

	public function getarticle($aid=6)
	{    
		 $sql = 'select * from '. self::TB_ARTICLE .' where article_id='.$aid;
         $result = $this->db->getone($sql); 
         return $result;
	} 

	public function getsortlist($sortid=0, $gsortid=0, $page_size=25, $keyword=''){
		if ($sortid) {
			$where .=" and arti_sortid=$sortid";
		}
		if ($gsortid) {
			$where .=" and arti_gsortid=$gsortid";
		}
		if ($keyword) {
			$where .=" and content like '%$keyword%'";
		}
        //echo $where;
		$sql = "select article_id, title, time from ".self::TB_ARTICLE." where 1=1".$where." order by time desc";
		//echo $sql;
		$result  = $this->db->pageQuery( $sql,$page_size); 	
		//print_r($result);
		return array(
				'result'	=> $result,
				'pagebar'	=> $this->db->getPageToolbar(0,false)
		);
	}
	public function get_sort_arr()
	{
		$sql="select * from ". self::TB_SORT;
        $row = $this->db->query( $sql);
		$result = $row->fetchAll( PDO::FETCH_ASSOC);
        return $result;		
	}
	public function get_gsort_arr()
	{
		$sql="select * from ". self::TB_GSORT;
        $row = $this->db->query( $sql);
		$result = $row->fetchAll( PDO::FETCH_ASSOC);
        return $result;		
	}
	public function get_flashpic($gsortid)
	{
		$sql="select * from ". self::TB_ARTICLE_FRONTPAGE ." where arti_gsortid=$gsortid and flash_pic=1 order by time desc limit 5";
		//echo $sql;
        $row = $this->db->query( $sql);
		$result = $row->fetchAll( PDO::FETCH_ASSOC);
        return $result;		
	}
	/**
	 * 组合形成站点数据
	 * @access public
	 * @param $uid int 用户ID
	 * @param $siteDomain  string 站点域名
	 * @return array
	 */ 
	public function assemStyle($tplparam='',$styleparam='',$userparam=''){
        $a_tplparam=($tplparam)?unserialize($tplparam):null;
       $a_styleparam=($styleparam)?unserialize($styleparam):null;
       $a_userparam=($userparam)?unserialize($userparam):null;
       $style=array();
       // print_r($a_tplparam);
      //  print_r($a_styleparam);
     //  print_r($a_userparam);
 
       for($i=0;$i<count($a_tplparam);$i++){ 
 
       	/*优先匹配用户自定义样式*/
       	  for($j=0;$j<count($a_userparam);$j++){  	
       	    if($a_tplparam[$i]['name']==$a_userparam[$j]['name']){
             	$style[$a_tplparam[$i]['name']]=$a_userparam[$j]['value'];
             	break;
                }	 
       	  }
       	  
       	 
       	  /*匹配样式参数*/
       	  if(!$style[$a_tplparam[$i]['name']]){
       	   for($l=0;$l<count($a_styleparam);$l++){  	
       	    if($a_tplparam[$i]['name']==$a_styleparam[$l]['name']){
             	$style[$a_tplparam[$i]['name']]=$a_styleparam[$l]['value'];
             	break;
                }	 
       	   } 
       	  }
 
       	  /*当以上两项为空时，*/
       	  if(!$style[$a_tplparam[$i]['name']]){
       	  		$style[$a_tplparam[$i]['name']]=$a_tplparam[$i]['default']; 
       	  }
 
       	    
       }
       return $style;
    }
     

 /**
  * 取出站点列表
  *	@access public 
  * @param  int $sortId 站点分类id
  * @param  int $number 
  * @return array | null
  */
  public function getSiteList($sortId, $number=6, $orderby='siteCreateTime')
  {
	  if ($sortId) {
		  $string = " and siteSortId=$sortId";
	  } 
	 
		   $sql = sprintf("select * from u_content_site");
		 // echo $sql;die;
          $row = $this->db->query( $sql);
		  $result = $row->fetchAll( PDO::FETCH_ASSOC);
		  return $result;
	  
  }

 /**
  * 取出商品列表
  *	@access public 
  * @param  int $proType 商品分类
  * @param  int $number 
  * @return array | null
  */
    public function getProLists($number=6, $proType='')
	{
		if ($proType) {
			$string = ' and (prodReco & '.pow(2,($proType-1)).' >0)';
		}
		$sql = "select prodId, prodName, prodMarketPrice, prodCommonPrice, prodVipPrice, prodVipVisibility, attcPath from ". self::TB_PRODUCT ." a left join ". self::TB_ATTACHMENT ." b on(a.prodiPicId=b.attcId) where prodStatus=1".$string." order by prodOrderTime desc limit $number";
        $row = $this->db->query( $sql);
		$result = $row->fetchAll( PDO::FETCH_ASSOC);
		return $result;
	}

 /**
  * 取出文章列表
  *	@access public 
  * @param  int $artiIsRoc 文章分类
  * @param  int $number 
  * @return array | null
  */
    public function getArticList($number=5, $artiIsRoc=0)
	{
		$sql = "select artiId, artiTitle,siteDomain from tb_article left join tb_site on artiObjId=siteMembId where artiState=1 and artiIsRoc=$artiIsRoc and siteMembId>0 order by artiTime desc limit $number";
        $row = $this->db->query( $sql);
		$result = $row->fetchAll( PDO::FETCH_ASSOC);
		return $result;
	}

	function getcookie() {
		$domain=$_SERVER['HTTP_HOST'];
		$sql="select apply_id, site_id from u_content_site a left join u_apply b on (a.user_account=b.user_account) where site_domain='$domain'";
		//echo $sql;die;
		$result = $this->db->getOne( $sql);
		return $result;

	}




 /**
  * 统计站点个数
  *	@access public 
  * @return array | null
  */
  public function countSite()
  {
	    $sql = "select count(siteId) as number from tb_site where siteState=1";
        $result = $this->db->getOne( $sql);
		//$result = $row->fetchAll( PDO::FETCH_ASSOC);
		return $result;
  }

	/**
	 * 根据站点ID信息
	 * @access public
	 * @return array
	 */	
	 public function siteInfo($siteId)
	 {  
		$sql="select a.*, b.merbPorInsName, b.merbPorInsInfo from ". self::TB_SITE ." a left join ". self::TB_MEMB_INFO_POR ." b on (a.siteMembId=b.merbId) where siteId=$siteId";
		//echo $sql;die;
		$result = $this->db->getone($sql);
		return $result;
	 }

 /**
  * 得到文章资源 
  * @param  $aritId  int   文章Id
  * @return array | null
  */
  public function getArtiCon($artiId)
  {   
	  $sql = "select * from ". self::TB_ARTICLE ." a left join ".self::TB_ARTICLE_TEXT." b on (a.artiId=b.artiId) where a.artiId=$artiId limit 1";
	  //echo $sql;die;
	  $result = $this->db->getOne($sql);
	  return  $result; 
  }


 /**
  * 取出店家的荣誉证书等信息，这些信息用于形成相应的页面
  *	@access public 
  * @param  int $com_id 店家编号ID
  * @return array | null
  */
 public function getCreditInfo( $cid,$page_size=5)
 {	
	$cid = intval($cid);
	if( $cid !=''){
		$sql = sprintf("select * from tb_article,tb_attachment where artiObjId=$cid and artiPorType=2 and attcId=artiPicId order by artiTime desc");
		//echo $sql;die;
		$result  = $this->db->pageQuery( $sql,$page_size); 	
		//print_r($result);
		return array(
				'result'	=> $result,
				'pagebar'	=> $this->db->getPageToolbar()
		);
	}
 }	

 /**
  * 改变网站点击数
  *	@access public 
  * @param  int $siteId 站点id
  * @return blooen
  */
  public function updateSiteHit($siteId)
  {
     $sql = sprintf("update tb_site set siteHit=siteHit+1 where siteId=%d",$siteId);
	 //echo $sql;die;
	 $result = $this->db->exec($sql);
	 return $result;
  }
	/**
	 * 根据域名得到站点信息
	 * @access public
	 * @return array
	 */	
	 public function getSiteInfos()
	 {   
	 	$domain = "http://".$_SERVER['HTTP_HOST'];
		$sql="select * from tb_site left join tb_site_style on styleId=siteStyleId left join tb_tpl on tplId=styleTplId where siteDomain='$domain'";
		//echo $sql;die;
		$result = $this->db->getone($sql);
		return $result;
	 }
	 
	 
}
?>